SUBROUTINE READPAR
!
! Verwendungszweck:
!     Einlesen des Inputfiles fuer den 1D-SHD Code,
!     Ueberpruefung der Plausibilitaet der Parameter,
!     Anlegen des Logfiles
!
!
!-----------------------------------------------------------------------
! Interne Variable:
!
!     logfile      : path & name des Logfiles
!     logfilelen   : genutzte Wortlaenge in "logfile"
!     path         : Pfad fuer die Ergebniss-Files
!     name         : Bezeichnung der Rechnung = Namensbasis fuer die Ergebniss-Files
!     line         : scratch Variable fuer die Umspeicherung des Input-Files in das Logfile
!     namelen      : genutzte Wortlaenge in "name"
!     pathlen      : genutzte Wortlaenge in "path"
!     lineNo       : Schleifenindex
!     modnrchar    : Die Modellnummer als character-variable
!     modnrcharlen : Die Laenge von "modnrchar"
!     string       : temporaerer string zum flexiblen Einlesen von Zahlenwerten, unabhaengig vom Variablentyp
!     archive_reference : Name des zum Kompilat gehoerigen Archiv zip-files
!     backup_reference  : Name des letzten vollstaendigen Backups


      use config,  only : np
      use global,  only : relax, extrapol, eps_G, eps_DX, DXmax, datfile, logfilename, &
                          modnr, statwindow, maxmod, t, tmax, tst, tstmin, tstlimit, zz, loglevel, it_strd, rmvmax, monokonv
      use physco,  only : z0, z1
      use gridvar, only : GW_MD, GW_ME, GW_OPA, beta, taurel
      use viscvar, only : q_lin_r, q_quad_r, q_lin_p, q_lin_p_ene, q_quad_p
      use matvar,  only : Planck, EOSfile, OPArosfile, OPAplkfile, gamma, xmol
      use zvar,    only : pseudo2D, z_gamma
      use outvar,  only : save_dat, save_tpd
      use RBvar,   only : H_star, f_irr

      implicit none

      character(len=50)  :: path, name
      character(len=112) :: logfile  ! 2 mal 50 +".???????"+".log"
      character(len=100) :: line
      character(len=9)   :: modnrchar
      character(len=31)  :: archive_reference
      character(len=29)  :: backup_reference
      character(len=10)  :: string
      integer            :: namelen, pathlen, modnrcharlen, logfilelen, lineNo


!-----------------------------------------------------------------------
!  Lesen des Inputfiles
!-----------------------------------------------------------------------


      read(5, "(28(/))")

      read(5,"(12x,a10)") string;   read(string,*) extrapol
      read(5,"(12x,a10)") string;   read(string,*) monokonv
      read(5,"(12x,a10)") string;   read(string,*) eps_G
      read(5,"(12x,a10)") string;   read(string,*) eps_DX
      read(5,"(12x,a10)") string;   read(string,*) it_strd
      read(5,"(12x,a10)") string;   read(string,*) zz
      read(5,"(12x,a10)") string;   read(string,*) DXmax
      read(5,"(12x,a10)") string;   read(string,*) rmvmax

      read(5,*)

      read(5,"(12x,a10)") string;   read(string,*) relax
      read(5,"(12x,a10)") string;   read(string,*) modnr
      read(5,"(12x,a10)") string;   read(string,*) maxmod
      read(5,"(12x,a10)") string;   read(string,*) tmax
      read(5,"(12x,a10)") string;   read(string,*) tst
      read(5,"(12x,a10)") string;   read(string,*) tstmin
      read(5,"(12x,a10)") string;   read(string,*) tstlimit

      read(5, "(3(/))")

      read(5,"(12x,a10)") string;   read(string,*) pseudo2D

      read(5,*)

      read(5,"(12x,a10)") string;   read(string,*) q_lin_r
      read(5,"(12x,a10)") string;   read(string,*) q_quad_r
      read(5,"(12x,a10)") string;   read(string,*) q_lin_p
!~       read(5,"(12x,a10)") string;   read(string,*) q_lin_p_ene
      read(5,"(12x,a10)") string;   read(string,*) q_quad_p

      read(5, "(3(/))")

      read(5,"(12x,a10)") string;   read(string,*) GW_MD
      read(5,"(12x,a10)") string;   read(string,*) GW_ME
      read(5,"(12x,a10)") string;   read(string,*) GW_OPA
      read(5,"(12x,a10)") string;   read(string,*) beta
      read(5,"(12x,a10)") string;   read(string,*) taurel

      read(5, "(3(/))")

      read(5,"(12x,a10)") string;   read(string,*) Planck
      read(5,"(12x,a10)") string;   read(string,*) gamma
      read(5,"(12x,a10)") string;   read(string,*) xmol
      read(5,"(12x,a10)") string;   read(string,*) z_gamma
      read(5,"(12x,a50)") EOSfile
      read(5,"(12x,a50)") OPArosfile
      read(5,"(12x,a50)") OPAplkfile

      read(5, "(3(/))")

      read(5,"(12x,a50)") name
      read(5,"(12x,a50)") path
      read(5,"(12x,a10)") string;   read(string,*) loglevel
      read(5,"(12x,a10)") string;   read(string,*) save_tpd
      read(5,"(12x,a10)") string;   read(string,*) save_dat
      read(5,"(12x,a10)") string;   read(string,*) statwindow


!-----------------------------------------------------------------------
!  Ueberpruefung der Plausibilitaet der Parameter
!-----------------------------------------------------------------------

      if (modnr >= maxmod) then
         write(*,"(a)")"readpar.f90:   Nummer des Startmodells groesser als erlaube Modellanzahl -> STOP"
         STOP
      end if

      if (t >= tmax) then
         write(*,"(a)")"readpar.f90:   Startzeit groesser als erlaube Maximalzeit -> STOP"
         STOP
      end if

      !debugging, bisher noch nirgends definiert
      H_star = z0
      if (pseudo2D) then
         f_irr = z1
      else      !  Turn off cooling and heating terms for plain 1D calculations
         f_irr = z0
      end if

      q_lin_p_ene = q_lin_p    ! gleicher Viskositaets Parameter fuer die Energiegleichung
      if(relax)  q_lin_p  = z0 ! Beim Relaxieren keine Scherviskositaet in der Bewegungsgleichung

      if(relax)  save_dat = 0  ! beime Relaxieren werden keine Modelle rausgeschrieben
                               ! Modelle mit nummern sind fuer Simulationsrechnungen vorbehalten

!-----------------------------------------------------------------------
!  Zusammensetzen der File-Namens des Logfiles
!-----------------------------------------------------------------------

      name = adjustl( name )       ! nach links schieben -> keine Blanks vorne
      namelen = len_trim ( name )  ! Laenge von "name" ohne hintere Blanks


      path = adjustl( path )
      pathlen = len_trim ( path )

      if(relax) then
         logfile = path(1:pathlen) // name(1:namelen) // ".rel.log"
         logfilelen = pathlen + namelen + 8
         logfilename = name(1:namelen) // ".rel.log"
      else
         write(modnrchar,"(I9)") modnr
         modnrchar = adjustl( modnrchar )
         modnrcharlen = len_trim( modnrchar )

         logfile = path(1:pathlen) // name(1:namelen) // "." // modnrchar(1:modnrcharlen) // ".log"
         logfilelen = pathlen + namelen + modnrcharlen + 5
         logfilename = name(1:namelen) // "." // modnrchar(1:modnrcharlen) // ".log"
      end if

      datfile = path(1:pathlen) // name(1:namelen)
      datfile = adjustl( datfile )

      open(unit=66,file=logfile,action="write",status="new",err=999)


!-----------------------------------------------------------------------
!  Kopieren des Input-Files in das Logfile
!-----------------------------------------------------------------------

      rewind(unit=5)

      do lineNo = 1,100   ! maximale angenommene Zeilenanzahl des Input-Files
         read(5,"(A100)") line
         write(66,"(A100)") line
      end do

      close(unit=5)

      include '../../common-code/archive-reference.txt'
      include '../../common-code/backup-reference.txt'

      write(66,"(a)")     "********************************************************************"
      write(66,"(a,a,a)") "*   corresponding code archive : ", archive_reference,         "   *"
      write(66,"(a,a,a)") "*   last full backup           : ", backup_reference,        "     *"
      write(66,"(a)")     "********************************************************************"
      write(66,"(a)")     " "


RETURN

999   continue

      ! Output ins Logfile ist naheliegender Weise nicht moeglich !
      write(*,"(a,a,a)")"readpar.f90:   Fehler beim Oeffnen der Logfiles: ",logfile(1:logfilelen)," -> STOP"
      STOP


END SUBROUTINE READPAR

